package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.easyder.wrapper.base.view.WrapperStatusActivity;
import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.contact.vo.GroupInfoVo;
import net.gyapp.markim.utils.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/4.
 * Desc:
 */

public class KeywordShieldActivity extends WrapperStatusActivity<CommonPresenter> {

    @BindView(R.id.et_content)
    EditText etContent;

    String groupNumber;

    public static Intent getIntent(Context context, String groupNumber) {
        return new Intent(context, KeywordShieldActivity.class).putExtra("groupNumber", groupNumber);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_keyword_shield;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        super.initView(savedInstanceState, titleView, intent);
        titleView.setCenterText("关键词屏蔽");
        groupNumber = intent.getStringExtra("groupNumber");
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        getGroupInfo(groupNumber, false);
    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        super.showContentView(url, dataVo);
        if (url.contains(ApiConfig.API_SET_GROUP_KEYS)) {
            showToast("设置成功");
            finish();
        } if (url.contains(ApiConfig.API_GET_GROUP_INFO)) {
            processGroupInfo((GroupInfoVo) dataVo);
        }
    }

    private void processGroupInfo(GroupInfoVo dataVo) {
        etContent.setText(dataVo.groupInfo.groupKeywordsFilter);
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String s = etContent.getText().toString();
        if (TextUtils.isEmpty(s)) {
            showToast("请输入要屏蔽的关键词");
            return;
        }

        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_SET_GROUP_KEYS, new RequestParams().put("groupNumber", groupNumber).put("keys", s).get(), BaseVo.class);
    }

    /**
     * 获取群信息
     *
     * @param groupNumber
     */
    private void getGroupInfo(String groupNumber, boolean showProgress) {
        presenter.setNeedDialog(showProgress);
        presenter.postData(ApiConfig.API_GET_GROUP_INFO, new RequestParams().put("groupNumber", groupNumber).get(), GroupInfoVo.class);
    }
}
